<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>21  vue数组</title>
    <script src="./js/vue.js"></script>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .table{
            width: 400px;
            height: auto;
            margin: 0px auto;
        }
        .table .item{
            float: left;
            width: 100px;
            background-color: midnightblue;
            color: mistyrose;
            line-height: 50px;
            height: 50px;
            text-align: center;
        }
        .add{
            width: 500px;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="add">
            用户：<input type="text" v-model = "username">
            年龄：<input type="text" v-model = "userage">
            <button @click = "add">新增</button>
        </div>
        <div class="table">
            <div class="tr">
                <div class="item">id</div>
                <div class="item">姓名</div>
                <div class="item">性别</div>
                <div class="item">操作</div>
            </div>
            <div class="tr"  v-for =  "item in list"  :key = "item.id">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item" @click = "del(item.id)">删除</div>
            </div>
        </div>
    </div>
    
</body>
    <script>
        new Vue({
            el : "#app",
            data : {
                list : [{id : 1 , username : "sean" , userage : 18} , {id : 2 , username : "lulu" , userage : 18} , {id : 3 , username : "lala" , userage : 18}],
                username : "",
                userage : ""
            },
            methods : {
                del(id){
                    this.list = this.list.filter(item => item.id != id)
                },
                add(){

                    if(this.list.some(item => item.username == this.userage)){
                        alert("用户名已经存在");
                    }else{
                        this.list.push({
                            id : this.list.length >= 1?this.list[this.list.length-1].id+1:1,
                            username : this.username,
                            userage : this.userage
    
                        })
                    }
                }

            },
            watch : {

            }
        })

    </script>
</html>